package br.com.gbvbahia.mybooks.fragment;

import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import br.com.gbvbahia.commons.Transacao.Transacao;
import br.com.gbvbahia.commons.Transacao.TransacaoFragmentTask;
import br.com.gbvbahia.commons.utils.AndroidUtils;
import br.com.gbvbahia.mybooks.R;

public class FragmentMae extends Fragment{
	private TransacaoFragmentTask task;
	
	protected void alert(int mensagem) {
		AndroidUtils.alertDialog(getActivity(), mensagem, R.string.app_name);
	}
	
	protected void alert(String mensagem) {
		AndroidUtils.alertDialog(getActivity(), mensagem, R.string.app_name);
	}
	
	// Inicia a thread
	public void startTransacaoComRede(Transacao transacao) {
		boolean redeOk = AndroidUtils.isNetworkAvailable(getActivity());
        if(redeOk) {
        	// Inicia a trans��o
    		task = new TransacaoFragmentTask(this, transacao , R.string.app_name, R.string.aguarde);
    		task.execute();        	
        } else {
        	// N�o existe conex�o
        	AndroidUtils.alertDialog(getActivity(), R.string.aviso_13, R.string.app_name);
        }
	}
	// Inicia a thread
	public void startTransacao(Transacao transacao) {
			TransacaoFragmentTask task = new TransacaoFragmentTask(this,transacao, R.string.app_name, R.string.aguarde);
			task.execute();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		if(task != null) {
			boolean executando = task.getStatus().equals(AsyncTask.Status.RUNNING);
			if(executando) {
				task.cancel(true);
				task.fecharProgress();
			}
		}
	}
}
